package com.zkthink.ceres.applet.check.service.impl;

import com.zkthink.base.R;
import com.zkthink.ceres.applet.check.domain.vo.SignVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.check.mapper.CheckRecordMapper;
import com.zkthink.ceres.applet.check.domain.entry.CheckRecord;
import com.zkthink.ceres.applet.check.service.CheckRecordService;

import javax.annotation.Resource;

import java.util.Date;

import static com.zkthink.ceres.applet.authority.config.AuthorityWebConfiguration.getMid;

/**

@Author cmz
@Date 2024-05-18 11:27
*/
@Service
public class CheckRecordServiceImpl extends ServiceImpl<CheckRecordMapper, CheckRecord> implements CheckRecordService{
    @Resource
    private CheckRecordMapper checkRecordMapper;
    @Override
    public R findMyToDayIsCheck() {
        Long mid = getMid();
        CheckRecord checkRecord = checkRecordMapper.findMyToDayIsCheck(mid);
        return R.success(checkRecord!=null);
    }

    @Override
    public R sign(SignVo signVo) {
        Long mid = getMid();
        CheckRecord checkRecord = checkRecordMapper.findMyToDayIsCheck(mid);
        if (checkRecord==null) {
            checkRecord = new CheckRecord();
            checkRecord.setMid(mid);
            BeanUtils.copyProperties(signVo, checkRecord);
            Date date = new Date();
            checkRecord.setCheckTime(date);
            checkRecord.setCreateTime(date);
            checkRecord.setCreateUser(String.valueOf(mid));
            int i = checkRecordMapper.insert(checkRecord);
            return i > 0 ? R.success() : R.fail("签到失败");
        }
        return R.fail("已签到，不能重复签到");
    }
}
